/**
 * Copyright (c) 2006-2016 Huize Ltd. All Rights Reserved. 
 *  
 * This code is the confidential and proprietary information of   
 * Hzins. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Huize,http://www.huize.com.
 *  
 */
package com.agan.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * 比如对于马拉松比赛，进行排名计算，参赛者的排名，肯定是跑完比赛之后，进行计算得出的，翻译成Java识别的预发，就是N个线程执行操作，
 * 主线程等到N个子线程执行完毕之后，在继续往下执行。
 */
public class CountDownLatchTest {

    public static void main(String[] str) {
	int threadCount = 10;
	final CountDownLatch latch = new CountDownLatch(threadCount);
	for (int i = 0; i < threadCount; i++) {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    System.out.println("线程" + Thread.currentThread().getId() + "开始出发");
		    try {
			Thread.sleep(1000);
		    } catch (InterruptedException e) {
			e.printStackTrace();
		    }
		    System.out.println("线程" + Thread.currentThread().getId() + "已到达终点");
		    latch.countDown();
		}
	    }).start();
	}

	try {
	    latch.await();
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}

	System.out.println("10个线程已经执行完毕！开始计算排名");
    }
}
